package cn.tedu.four.web;

import cn.tedu.four.ex.ServiceException;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import lombok.experimental.Accessors;
import springfox.documentation.spring.web.json.Json;

/**
 * ClassName: JsonResult
 * Package: cn.tedu.four.web
 * Description:
 *
 * @Author: @Albert
 * @Version: 1.0
 */
@Data
@Accessors(chain=true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class JsonResult {
    private Integer state;
    /*操作失败的响应消息*/
    private String message;
    /*操作成功时候的响应数据*/
    private Object data;

    /*之所以设置成静态的,是因为  非静态方法需要先构建对象才能调用*/
    public static JsonResult ok(){
//        JsonResult js = new JsonResult();
//        js.state = ServiceCode.OK.getValue();
//        return js;
        return JsonResult.ok(null);
    }
    public static JsonResult ok(Object data){
        JsonResult js = new JsonResult();
        js.state = ServiceCode.OK.getValue();
        js.data = data;
        return js;
    }
    public static JsonResult fail(ServiceException e){
//        JsonResult js = new JsonResult();
//        js.state = e.getServiceCode().getValue();
//        js.message = e.getMessage();
//        return  js;
        return JsonResult.fail(e.getServiceCode(),e.getMessage());
    }
    public static JsonResult fail(ServiceCode serviceCode,String message){
        JsonResult js = new JsonResult();
        js.state = serviceCode.getValue();
        js.message = message;
        return  js;
    }
}
